import Vue from 'vue'
import confirmBox from './ConfirmBox'
const defaults = {
  showBox: false,
  showBtn:{
    showConfirm: true,
    showCancel: true
  },
  title: '',
  text: '',
  button: []

}
let boxLoading
const confirmBoxConstructor = Vue.extend(confirmBox)
confirmBoxConstructor.prototype.close = function () {
  var vm = this
  boxLoading = null
  var promise = new Promise(function (resolve, reject) {
    if (vm.$el && vm.$el.parentNode) {
      vm.$el.parentNode.removeChild(vm.$el)
    }
    resolve()
    vm.$destroy()
    vm.showBox = false
  })
  return promise
}
const confirm = (options = {}) => {
  if (Vue.prototype.$isServer) return
  options = Object.assign({}, defaults, options)
  let parent = document.body
  if (boxLoading) {
    boxLoading.close()
    // return boxLoading
  }
  let instance = new confirmBoxConstructor({
    el: document.createElement('div'),
    data: options
  })

  parent.appendChild(instance.$el)
  Vue.nextTick(() => {
    instance.showBox = true
  })

  boxLoading = instance
  return instance
}

export default confirm
